package com.javakc.copyrightin.chapter.service;

import com.javakc.commonutils.jpa.base.service.BaseService;
import com.javakc.commonutils.jpa.dynamic.SimpleSpecificationBuilder;
import com.javakc.copyrightin.book.entity.Book;
import com.javakc.copyrightin.chapter.dao.ChapterDao;
import com.javakc.copyrightin.chapter.entity.Chapter;
import com.javakc.copyrightin.chapter.vo.ChapterQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service

public class ChapterService extends BaseService<ChapterDao,Chapter> {

    @Autowired
    private ChapterDao chapterDao;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public Page<Chapter> findPage(int pageNo, int pageSize, ChapterQuery chapterQuery){
        /**
         * 可用操作符
         * = 等值、!= 不等值 (字符串、数字)
         * >=、<=、>、< (数字)
         * ge，le，gt，lt(字符串)
         * :表示like %v%
         * l:表示 v%
         * :l表示 %v
         * null表示 is null
         * !null表示 is not null
         */
        SimpleSpecificationBuilder simpleSpecificationBuilder = new SimpleSpecificationBuilder();
        if(StringUtils.hasLength(chapterQuery.getName())){
            simpleSpecificationBuilder.and("name",":",chapterQuery.getName());
        }
        if(!ObjectUtils.isEmpty(chapterQuery.getStatus())) {
            simpleSpecificationBuilder.and("status", "=", chapterQuery.getStatus());
        }
        if(!ObjectUtils.isEmpty(chapterQuery.getDate1())){
            simpleSpecificationBuilder.and("updateTime","ge",sdf.format(chapterQuery.getDate1())+new Date(86400));
        }
        if(!ObjectUtils.isEmpty(chapterQuery.getDate2())){
            simpleSpecificationBuilder.and("updateTime","le",sdf.format(chapterQuery.getDate2())+new Date(86400));
        }
        Page<Chapter> page = dao.findAll(simpleSpecificationBuilder.getSpecification(), PageRequest.of(pageNo - 1,pageSize));
        return page;
    }

    @Transactional
    public void insert(Integer id){
        chapterDao.insert(id);
    }


}
